package com.finiac.controller;

import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Component;
import org.springframework.ui.ModelMap;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

import com.finiac.dao.FeeRegisterDAO;
import com.finiac.dao.FeeStructureDAO;


import com.finiac.model.FeeRegister;
import com.finiac.model.FeeStructure;






public class FeeController extends MultiActionController {
	
	FeeStructureDAO feeStructureDAO;
	FeeRegisterDAO feeRegisterDAO;
	
	
	
	
	
	
	public void setFeeStructureDAO(FeeStructureDAO feeStructureDAO) {
		this.feeStructureDAO = feeStructureDAO;
	}
	public void setFeeRegisterDAO(FeeRegisterDAO feeRegisterDAO){
		this.feeRegisterDAO = feeRegisterDAO;
	}
	

	public ModelAndView listFeeStruct(HttpServletRequest request, HttpServletResponse response)throws Exception
	{
		
		ModelMap modelMap = new ModelMap();
		modelMap.addAttribute("feeStructList", feeStructureDAO.listAll());
		modelMap.addAttribute("feeStructure", new FeeStructure());
		return new ModelAndView("feeStructure",modelMap);
	}
	/*public ModelAndView addFeeStruct(HttpServletRequest request, HttpServletResponse response, FeeStructure feeStructure)throws Exception
	{
		feeStructure.setIsActive(1);
		if(feeStructure.getfeeStructureName()=="")
			return new ModelAndView("redirect:listFeeStruct.htm");	
		feeStructure.setTotal(feeStructure.getTutionFee()+feeStructure.getQuotionDepo()+feeStructure.getRefundableDepo()+feeStructure.getSpecialFee()+feeStructure.getMiscellaneous());
		feeStructureDAO.addFeeStruct(feeStructure);
		return new ModelAndView("redirect:listFeeStruct.htm");
		
	}*/
	public ModelAndView updateFeeStructurePage(HttpServletRequest request, HttpServletResponse response)throws Exception
	{
		int id=Integer.parseInt(request.getParameter("id"));
		ModelMap modelMap = new ModelMap();
		modelMap.addAttribute("feeStructureList", feeStructureDAO.selectById(id));
		modelMap.addAttribute("feeStructure", new FeeStructure());
		return new ModelAndView("updateFeeStructure",modelMap);
	}
/*public ModelAndView updateFeeStructure(HttpServletRequest request, HttpServletResponse response, Product product)throws Exception
	{
		float qtyPur=Float.parseFloat(request.getParameter("qtyPur"));
		float qtyAvail= product.getQtyAvail();
		product.setQtyAvail(qtyAvail+qtyPur);
		product.setIsActive(1);
		productDAO.updateProduct(product);
		return new ModelAndView("redirect:list.htm");
	}
*/
	
	public ModelAndView addDailyFeeRegPage(HttpServletRequest request,HttpServletResponse response, FeeRegister feeRegister)throws Exception
	{
		return new ModelAndView("dailyFeeRegister");
	}
	public ModelAndView addDailyFeeReg(HttpServletRequest request,HttpServletResponse response, FeeRegister feeRegister) throws Exception {
		feeRegister.setIsActive(1);
		feeRegisterDAO.addFeeRegister(feeRegister);
		return new ModelAndView("redirect:listDailyFeeReg.htm");
	}
	
	public ModelAndView autoComStudent(HttpServletRequest request, HttpServletResponse response)throws Exception
	{
		long admissionNo= Long.parseLong(request.getParameter("adNo"));
		FeeStructure feeStructure = new FeeStructure();
		feeStructure= feeStructureDAO.selectByAdmissionNo(admissionNo);
		return new ModelAndView("autoCompleteStudent", "feeStructure", feeStructure);
	}
	
	public ModelAndView listDailyFeeReg(HttpServletRequest request,HttpServletResponse response) throws Exception {
			ModelMap modelMap = new ModelMap();
			modelMap.addAttribute("dailyList", feeRegisterDAO.listAll());
			modelMap.addAttribute("feeRegister", new FeeRegister());
			return new ModelAndView("dailyFeeRegister",modelMap);
	}
	
	
	
	
	public ModelAndView addStudentFeeStructurePage(HttpServletRequest request,HttpServletResponse response, FeeStructure feeStructure)throws Exception
	{
		return new ModelAndView("studentFeeStructure");
	}
	public ModelAndView addStudentFeeStructure(HttpServletRequest request,HttpServletResponse response, FeeStructure feeStructure)throws Exception
	{
		feeStructure.setIsActive(1);
		feeStructureDAO.addFeeStructure(feeStructure);
		return new ModelAndView("studentFeeStructure");
	}
	
	public ModelAndView addRegularFeeRegPage(HttpServletRequest request,HttpServletResponse response, FeeStructure feeStructure)throws Exception
	{
		return new ModelAndView("regularFeeRegister");
	}
	public ModelAndView addRegularFeeReg(HttpServletRequest request,HttpServletResponse response, FeeStructure feeStructure)throws Exception
	{
		feeStructure.setIsActive(1);
		/*feeStructure.setTotal(feeStructure.getTutionFee()+feeStructure.getSpecialFee()+feeStructure.getQuotionDepo()+feeStructure.getRefundableDepo()+feeStructure.getMiscellaneous()+feeStructure.getAmount());
		*/feeStructureDAO.addFeeStructure(feeStructure);
		return new ModelAndView("redirect:listRegularFeeReg.htm");
		}
	public void deleteRegularFeeReg(HttpServletRequest request, HttpServletResponse response)throws Exception
	{
		int id=Integer.parseInt(request.getParameter("id"));
		FeeStructure feeStructure = new FeeStructure();
		List<FeeStructure> regularList= feeStructureDAO.selectById(id);
		for(int i=0;i<regularList.size();i++)
		{
			feeStructure = regularList.get(i);
		}
		feeStructure.setIsActive(0);
		feeStructureDAO.updateFeeStructure(feeStructure);
	}
	
	public ModelAndView listRegularFeeReg(HttpServletRequest request,HttpServletResponse response) throws Exception {
		ModelMap modelMap = new ModelMap();
		modelMap.addAttribute("regularList", feeStructureDAO.listAll());
		modelMap.addAttribute("feeStructure", new FeeStructure());
		return new ModelAndView("regularFeeRegister",modelMap);
}
	
	/*public ModelAndView listStudentFee(HttpServletRequest request,HttpServletResponse response) throws Exception {
		ModelMap modelMap = new ModelMap();
		modelMap.addAttribute("feeStructList", feeStructureDAO.listAll());
		modelMap.addAttribute("feeStructure", new FeeStructure());
		return new ModelAndView("consolStudentFeeStructure",modelMap);
}*/
	public ModelAndView listStudentFee(HttpServletRequest request, HttpServletResponse response)throws Exception
	{
		long admissionNo;
		if(request.getParameter("admissionNo")==null)
			admissionNo=0;
		else
			admissionNo=Long.parseLong(request.getParameter("admissionNo"));
		ModelMap modelMap = new ModelMap();
		modelMap.addAttribute("feeList", feeStructureDAO.selectByStudent(admissionNo));
		modelMap.addAttribute("feeStructure", new FeeStructure());
		return new ModelAndView("consolStudentFeeStructure",modelMap);
	}
	
	public void deleteStudentFeeStructure(HttpServletRequest request, HttpServletResponse response)throws Exception
	{
		int id=Integer.parseInt(request.getParameter("id"));
		FeeStructure feeStructure = new FeeStructure();
		List<FeeStructure> feeList= feeStructureDAO.selectById(id);
		for(int i=0;i<feeList.size();i++)
		{
			feeStructure = feeList.get(i);
		}
		feeStructure.setIsActive(0);
		feeStructureDAO.updateFeeStructure(feeStructure);
	}
	
	
	
	/*public ModelAndView searchAdmissionNo(HttpServletRequest request,HttpServletResponse response, Student student)throws Exception
	{
		student.setAdmissionNo(admissionNo);
		
		
	}*/
	public ModelAndView consolDailyFeeReg(HttpServletRequest request, HttpServletResponse response)throws Exception
	{
		long admissionNo;
		if(request.getParameter("admissionNo")==null)
			admissionNo=0;
		else
			admissionNo=Long.parseLong(request.getParameter("admissionNo"));
		ModelMap modelMap = new ModelMap();
		modelMap.addAttribute("dailyList", feeRegisterDAO.selectByStudent(admissionNo));
		modelMap.addAttribute("feeRegister", new FeeRegister());
		return new ModelAndView("consolDailyFeeRegister",modelMap);
	}
	
	public void deleteDailyFeeReg(HttpServletRequest request, HttpServletResponse response)throws Exception
	{
		int id=Integer.parseInt(request.getParameter("id"));
		FeeRegister feeRegister = new FeeRegister();
		List<FeeRegister> dailyList= feeRegisterDAO.selectById(id);
		for(int i=0;i<dailyList.size();i++)
		{
			feeRegister = dailyList.get(i);
		}
		feeRegister.setIsActive(0);
		feeRegisterDAO.updateFeeRegister(feeRegister);
	}
	public ModelAndView updateDailyFeeReg(HttpServletRequest request, HttpServletResponse response)throws Exception
	{
		int id=Integer.parseInt(request.getParameter("id"));
		FeeRegister feeRegister = new FeeRegister();
		List<FeeRegister> dailyList= feeRegisterDAO.selectById(id);
		for(int i=0;i<dailyList.size();i++)
		{
			feeRegister = dailyList.get(i);
		}
		return new ModelAndView("updateDailyFeeReg","fee",feeRegister);
	}
	
	
	public ModelAndView addUpdateDailyFeeReg(HttpServletRequest request, HttpServletResponse response)throws Exception
	{
		int id=Integer.parseInt(request.getParameter("id"));
		long admissionNo=Long.parseLong(request.getParameter("admissionNo"));
		String name = request.getParameter("name");
		String branch = request.getParameter("branch");
		int yearOfAdmission = Integer.parseInt(request.getParameter("yearOfAdmission"));
		String category = request.getParameter("category");
		String date = request.getParameter("date");
		float amount =Float.parseFloat(request.getParameter("amount"));
		
		FeeRegister feeRegister = new FeeRegister();
		List<FeeRegister> dailyList= feeRegisterDAO.selectById(id);
		for(int i=0;i<dailyList.size();i++)
		{
			feeRegister = dailyList.get(i);
		}
		feeRegister.setId(id);
		feeRegister.setAdmissionNo(admissionNo);
		feeRegister.setName(name);
		feeRegister.setBranch(branch);
		feeRegister.setYearOfAdmission(yearOfAdmission);
		feeRegister.setCategory(category);
		feeRegister.setDate(date);
		feeRegister.setAmount(amount);
		
		feeRegisterDAO.updateFeeRegister(feeRegister);
		return new ModelAndView("redirect:listDailyFeeReg.htm");
	}
	public ModelAndView updateListDailyFeeReg(HttpServletRequest request, HttpServletResponse response)throws Exception
	{
		int id=Integer.parseInt(request.getParameter("id"));
		FeeRegister feeRegister = new FeeRegister();
		List<FeeRegister> dailyList= feeRegisterDAO.selectById(id);
		for(int i=0;i<dailyList.size();i++)
		{
			feeRegister = dailyList.get(i);
		}
		return new ModelAndView("updateListDailyFeeReg","fee",feeRegister);
	}
	public ModelAndView addUpdateListDailyFeeReg(HttpServletRequest request, HttpServletResponse response, FeeRegister feeRegister)throws Exception
	{
		feeRegister.setIsActive(1);
		feeRegisterDAO.updateFeeRegister(feeRegister);
		return new ModelAndView("redirect:consolDailyFeeRegister.htm");
	}
	}